<?php
class LinksController extends AppController{
	public $uses=array('Link');

	public $layout='design';

	function index(){


		if(isset($_POST['submit'])){
			if($_POST['tags']==""){
				echo "検索条件がありません";
				$linkData=$this->Link->find('all');
			}else{
				$tags=$_POST['tags'];
				$this->set('tags',$tags);
				$linkData=
				$this->Link->find(
						'all',
						array('conditions'=>array('l_descript like'=>"%{$tags}%"
						)
						));
			}
		}else{
			$linkData=$this->Link->find('all');
		}
		$this->set('linkData',$linkData);
	}

	public function edit($id=null){
		if($this->request->isPost()||$this->request->isPut()){
			if(!empty($this->data)){
				if($this->Link->save($this->data)){
					$this->Session->setFlash("Succeed");
					$this->redirect(array('action'=>'index'));
					return;
				}
			}
			$this->Session->setFlash('Faild');
		}else{
			if(!is_null($id)){
				$this->data=$this->Link->findById($id);
			}
		}

		$isEdit=false;

		if(!is_null($id)){
			$isEdit=true;
		}

		$this->set('isEdit',$isEdit);
	}

	public function add(){
		$this->edit();
		$this->render('edit');
	}

	public function delete($id){
		if($this->request->isDelete()){
			if(!empty($this->data)){
				if($this->Link->delete($this->data[$this->Link->alias]['id'])){
					$this->Session->setFlash('Succeed');
					$this->redirect(array('action'=>'index'));
					return;
				}
			}

			$this->Session->setFlash('Faild');
			$this->redirect(array('action'=>'index'));
			return;
		}else{
			$this->data=$this->Link->findById($id);
			$this->set('data',$this->data);
			if(empty($this->data)){
				$this->Session->setFlash('Data not found');
				$this->redirect(array('action'=>'index'));
				return;
			}
		}
	}
}